import { Injectable } from '@nestjs/common';
import { SendMessageDto } from './dto/send-message.dto';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Message } from './interfaces/message.interface';

@Injectable()
export class MessagesService {
  constructor(
    @InjectModel('Message') private readonly messageModel: Model<Message>,
  ) {}

  /**
   * 发送一条消息，给学生们
   * @param sendMessageDto 发送的消息
   */
  sendMessage(sender: string, sendMessageDto: SendMessageDto) {
    const createdMessage = new this.messageModel({ sender, ...sendMessageDto });
    return createdMessage.save();
  }

  /**
   * 获取所有消息
   */
  findMessage() {
    return this.messageModel.find({}, '-__v');
  }
}
